package cong.cheng.thread;

// 使用 wait() 方法的示例
public class WaitExample {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        Thread t = new Thread(() -> {
            try {
                System.out.println("Thread started");
                synchronized (lock) {
                    lock.wait(); // 线程暂停，直到被 notify() 或 notifyAll() 唤醒
                }
                System.out.println("Thread resumed");
            } catch (InterruptedException e) {
                System.out.println("Thread interrupted");
            }
        });
        t.start();
        Thread.sleep(1000); // 主线程等待1秒钟
        synchronized (lock) {
            lock.notify(); // 唤醒等待的线程
        }
        t.join(); // 等待线程执行完毕
    }
}